$(()=>{
    if(sessionStorage.getItem("msg_data")==null){
        alert("您的登录信息已过期请重新登陆");
        window.location.href="http://172.18.80.9:8087/admin/login/login"
    }
    var msg_data =JSON.parse(sessionStorage.getItem("msg_data"));
    console.log(msg_data.id)
    console.log(msg_data.token);
    var tid =location.search.split("add=")[1];
    //添加项目进入
    if(tid!=undefined){
       //论文类型
       $.ajax({
           type:"POST",
           data:{uid:msg_data.id,token:msg_data.token},
           url:"http://172.18.80.9:8087/admin/principal/sThesis",
           success:function(data){
               var msg=jQuery.parseJSON(data);
               if(msg.code==9){
                   alert(msg.msg)
                   window.location.href="http://172.18.80.9:8087/admin/login/login"
                   return false;
               }
               var papertype=[];
               for(var i=0;i<msg.data.length;i++){
                   papertype.push(msg.data[i]);
               }
               var paper=`<option>请选择论文类型</option>`;
               for(var d of papertype){
                   paper+=`<option value="${d.id}">${d.content}</option>`
               }
               $("#thesistype").html(paper);

           },
           error:function(err){
               console.log(err);
               alert("网络故障请检查");
           }
       });
       //请选择项目
       $.ajax({
           type: "POST",
           dataType:"json",
           data:{uid:msg_data.id, token:msg_data.token,},
           url: "http://172.18.80.9:8087/admin/research/pName",
           success: function (data) {
               var html=`<option value="">请选择项目</option>`;
               for(var d of data.data){
                   html+=`<option value="${d.project_id+"?"+d.project_name}">${d.project_name}</option>
                    `
               }
               $("#project").html(html);
           },
           error: function (err) {
               console.log(err);
               alert("网络故障请检查");
           }
       });
    }
    //编辑进入
    var n2=location.href.indexOf("id=");//取得=号的位置
    if(n2!=-1){
        var tid =location.search.split("id=")[1];
        //论文类型
        $.ajax({
            type:"POST",
            data:{uid:msg_data.id,token:msg_data.token},
            url:"http://172.18.80.9:8087/admin/principal/sThesis",
            success:function(data){
                var msg=jQuery.parseJSON(data);
                if(msg.code==9){
                    alert(msg.msg)
                    window.location.href="http://172.18.80.9:8087/admin/login/login"
                    return false;
                }
                var papertype=[];
                for(var i=0;i<msg.data.length;i++){
                    papertype.push(msg.data[i]);
                }
                var paper=`<option>请选择论文类型</option>`;
                for(var d of papertype){
                    paper+=`<option value="${d.id}">${d.content}</option>`
                }
                $("#thesistype").html(paper);
                //详情
                $.ajax({
                    type:"POST",
                    data:{id:tid,uid:msg_data.id,token:msg_data.token},
                    dataType:"json",
                    url:"http://172.18.80.9:8087/admin/research/dpThesis",
                    success:function(data){
                        for(d of data.data){
                            console.log(d)
                            //项目
                            $("#project>option:first-child").css("display","none")
                            $("#project").val(d.project_id+"?"+d.project_name);
                            //论文类型
                            $("#thesistype>option:first-child").css("display","none")
                            $("#thesistype").val(d.thesis_id);
                            //文章名称
                            $("#article_name").val(d.article_name);
                            //作者名称
                            $("#author").val(d.author)
                            //期刊名称
                            $("#periodical_name").val(d.periodical_name)
                            //时间
                            var str=d.pilot_date;
                            if(str ==null){
                                console.log("时间为null");
                            }else{
                                arr=str.split(" ");
                                var pilot_date = arr[0];
                            }
                            $("#pilot_date").val(pilot_date)
                        }
                    },
                    error:function(err){
                        console.log(err);
                        alert("网络故障请检查");
                    }
                });

            },
            error:function(err){
                console.log(err);
                alert("网络故障请检查");
            }
        });
        //请选择项目
        $.ajax({
            type: "POST",
            dataType:"json",
            data:{uid:msg_data.id, token:msg_data.token,},
            url: "http://172.18.80.9:8087/admin/research/pName",
            success: function (data) {
                var html=`<option value="">请选择项目</option>`;
                for(var d of data.data){
                    html+=`<option value="${d.project_id+"?"+d.project_name}">${d.project_name}</option>`
                }
                $("#project").html(html);

            },
            error: function (err) {
                console.log(err);
                alert("网络故障请检查");
            }
        });
    }
    //保存
    $(document).on("click","#save",function () {
        //文章名称
        var article_name=$("#article_name").val();
        //作者
        var author=$("#author").val();
        //时间
        var pilot_date=$("#pilot_date").val();
        //期刊名称
        var periodical_name=$("#periodical_name").val();
        //项目id
        var  project_id=[];
        var  project_name=[];
        //项目名称
        $("#project").each(function () {
            if($(this).val()==""){
                var b="";
                var c="";
            }
            if($(this).val()!=""){
                var b= $(this).val().split("?")[0];
                var c= $(this).val().split("?")[1];
            }
            project_id.push(b);
            project_name.push(c);
        })
        var projectid = project_id.join('');
        var projectname=project_name.join('');
        //论文类型id
        var thesistype=[];
        $("#thesistype").each(function(){
            if( $(this).val()=="请选择论文类型"){
                var a="";
            }else{
                var a= $(this).val();
            }
            thesistype.push(a);
        });
        var lid = thesistype.join('');
        //上传文件
        formData = new FormData();
        if( $('#imageFile')[0].files[0]==undefined){
            formData.append('imageFile'," ");
        }else{
            formData.append('imageFile', $('#imageFile')[0].files[0]);
        }
        formData.append('uid', msg_data.id);
        formData.append('token', msg_data.token);
        formData.append('thesis_id',lid);
        formData.append('id', tid);
        formData.append('article_name', article_name);
        formData.append('project_id', projectid);
        formData.append('author', author);
        formData.append('periodical_name', periodical_name);
        formData.append('pilot_date', pilot_date);
        formData.append('project_name', projectname);
        $.ajax({
            type: "POST",
            data: formData,
            cache: false,
            processData: false,
            contentType: false,
            url: "http://172.18.80.9:8087/admin/research/spThesis",
            success: function (data) {
                var data =JSON.parse(data)
                console.log(data)
                if(data.code==9){
                    alert(data.msg)
                    window.location.href="http://172.18.80.9:8087/admin/login/login"
                    return false;
                }
                if(data.code==2){
                    alert(data.msg)
                    console.log(data)
                }
                if(data.code==0){
                    alert(data.msg)
                  location.href="http://172.18.80.9:8087/admin/research/thesislist"
                }

            },
            error: function (err) {
                console.log(err);
                alert("网络故障请检查");
            }
        });
    })
    //提交
    $(document).on("click","#submit",function () {
        //项目id和项目名称
        var  project_id=[];
        var  project_name=[];
        $("#project").each(function () {
            if($(this).val()==""){
                var b="";
                var c="";
            }
            if($(this).val()!=""){
                var b= $(this).val().split("?")[0];
                var c= $(this).val().split("?")[1];
            }
            project_id.push(b);
            project_name.push(c);
        })
        var projectid = project_id.join('');
        var projectname=project_name.join('');
        if(projectid==""){
            alert("项目名称不能为空")
            return false
        }
        //论文类型id
        var thesistype=[];
        $("#thesistype").each(function(){
            if( $(this).val()=="请选择论文类型"){
                var a="";
            }else{
                var a= $(this).val();
            }
            thesistype.push(a);
        });
        var lid = thesistype.join('');
        if(lid==""){
            alert("论文类型不能为空")
            return false
        }
        //文章名称
        var article_name=$("#article_name").val();
        if(article_name==""){
            alert("文章名称不能为空")
            return false;
        }
        //作者
        var author=$("#author").val();
        if(author==""){
            alert("作者不能为空")
            return false;
        }
        //期刊名称
        var periodical_name=$("#periodical_name").val();
        if(periodical_name==""){
            alert("期刊名称不能为空")
            return false;
        }
        //时间
        var pilot_date=$("#pilot_date").val();
        if(pilot_date==""){
            alert("时间不能为空")
            return false;
        }


        //上传文件
        formData = new FormData();
        if( $('#imageFile')[0].files[0]==undefined){
           alert("上传文件不能为空")
            return false;
        }
        formData.append('imageFile', $('#imageFile')[0].files[0]);
        formData.append('uid', msg_data.id);
        formData.append('token', msg_data.token);
        formData.append('thesis_id',lid);
        formData.append('id', tid);
        formData.append('article_name', article_name);
        formData.append('project_id', projectid);
        formData.append('author', author);
        formData.append('periodical_name', periodical_name);
        formData.append('pilot_date', pilot_date);
        formData.append('project_name', projectname);
        $.ajax({
            type: "POST",
            data: formData,
            cache: false,
            processData: false,
            contentType: false,
            url: "http://172.18.80.9:8087/admin/research/bpThesis",
            success: function (data) {
                var data =JSON.parse(data)
                if(data.code==9){
                    alert(data.msg)
                    window.location.href="http://172.18.80.9:8087/admin/login/login"
                    return false;
                }
                if(data.code==2){
                    alert(data.msg)
                }
                if(data.code==0){
                    if(confirm("提交后不可修改，您确定要提交吗")){
                        alert("提交成功");
                        window.location.href="http://172.18.80.9:8087/admin/research/thesislist"
                    }else{
                        return false;
                    }



                }

            },
            error: function (err) {
                console.log(err);
                alert("网络故障请检查");
            }
        });
    })

})
